package com.chlitina.store.modules.inventory.utils;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.chlitina.store.modules.base.entity.StoreProduct;
import com.chlitina.store.modules.base.service.StoreProductService;
import com.chlitina.store.modules.inventory.entity.StoreInventoryCheckDetail;
import com.chlitina.store.modules.inventory.service.StoreInventoryCheckDetailService;

@Service
public class StoreInventoryUtils implements ApplicationContextAware{
	private static StoreProductService storeProductService;
	private static StoreInventoryCheckDetailService storeInventoryCheckDetailService;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		storeProductService= (StoreProductService) applicationContext.getBean("storeProductService");
		storeInventoryCheckDetailService= (StoreInventoryCheckDetailService) applicationContext.getBean("storeInventoryCheckDetailService");
		
	}
	public static StoreProduct getStoreProduct(Long id){
		if (id != null) {
			return storeProductService.get(id);
		}
		return null;
	}
	public static float getLostMoneyById(Long id){
		if (id == null) {
			return 0;
		}
		List<StoreInventoryCheckDetail> detailList = storeInventoryCheckDetailService.getByCheckId(id);
		float lost = 0;
		if (detailList != null && detailList.size() > 0) {
			for (StoreInventoryCheckDetail detail : detailList) {
				int before = detail.getBeforeAdjustAmount();
				int after = detail.getAfterAdjustAmount();
				float retailPrice = detail.getRetailPrice();
				lost += (before - after) * retailPrice;
			}
		}
		return lost;
	}
}
